package com.demo01.demo.controller;

import com.demo01.demo.Repository.UserRepository;
import com.demo01.demo.entity.Milktea;
import com.demo01.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test")
public class UserHandler {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/userfindAll/{page}/{size}")
    public Page<User> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return userRepository.findAll(pageable);
    }

    @GetMapping("/usernum")
    public long count(){
        return userRepository.count();
    }

    @PostMapping("/usersearch/{page}/{size}")
    public Page<User> search(@RequestBody User user, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        String[] sql = {"","","","",""};
        if (user.getId()!= null) {
            sql[0] = user.getId();
        }
        if (user.getName() != null) {
            sql[1] = user.getName();
        }
        if (user.getActive() != null) {
            sql[2] = user.getActive().toString();
        }
        if (user.getProvince() != null) {
            sql[3] = user.getProvince();
        }
        if (user.getCity() != null) {
            sql[4] = user.getCity();
        }
        return userRepository.find(sql[0],sql[1],sql[2],sql[3],sql[4],pageable);
    }

    @PostMapping("/usersearchall")
    public List<User> searchall(@RequestBody User user) {
        String[] sql = {"","","","",""};
        if (user.getId()!= null) {
            sql[0] = user.getId();
        }
        if (user.getName() != null) {
            sql[1] = user.getName();
        }
        if (user.getActive() != null) {
            sql[2] = user.getActive().toString();
        }
        if (user.getProvince() != null) {
            sql[3] = user.getProvince();
        }
        if (user.getCity() != null) {
            sql[4] = user.getCity();
        }
        return userRepository.searchall(sql[0],sql[1],sql[2],sql[3],sql[4]);
    }

    @PutMapping("/userupdate")
    public String update(@RequestBody User user) {
        User result = userRepository.save(user);
        if (result != null) {
            return "success";
        } else {
            return "error";
        }
    }

    @GetMapping("/userall")
    public List<User> findall(){
        return userRepository.findAll();
    }

    @GetMapping("/usersort/{page}/{size}")
    public Page<User> Sort(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Sort sort = Sort.by(Sort.Direction.ASC, "province");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return userRepository.findAll(pageable);
    }

    @GetMapping("/usersortall")
    public List<User> sortall() {
        Sort sort = Sort.by(Sort.Direction.ASC, "province");
        return userRepository.findAll(sort);
    }

    @RequestMapping("/adduser")
    public void adduser(String username,String province,String city){
        if(userRepository.findByName(username).isEmpty()) {
            User user = new User();
            user.setName(username);
            user.setId(String.valueOf(userRepository.count() + 1));
            user.setActive(1);
            user.setProvince(province);
            user.setCity(city);
            userRepository.save(user);
        }
    }

    @DeleteMapping("/userdeleteById/{user_id}")
    public void deleteById(@PathVariable("user_id") String id) {
        userRepository.deleteById(id);
    }
}
